/* Bump mapping functions
 *
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include "bump.h"
#include "hw6.h"

int calc_bezier_curve() {
	int i;
	float t;
	float minX, minY;
	float maxX, maxY;

	minX = minY = 0;
	maxX = maxY = 0;

	for (i = 0; i < MAX_POINTS; i++) {
		t = i / 99.;
		calc_bezier_point(bezier[i], t);

		if (bezier[i].c[X] < minX)
			minX=bezier[i].c[X];

   		if (bezier[i].c[X] > maxX)
   			maxX=bezier[i].c[X];

 		if (bezier[i].c[Y] < minY)
   			minY=bezier[i].c[Y];

   		if (bezier[i].c[Y] > maxY)
   			maxY=bezier[i].c[Y];
	}

	/* prevent self-intersection */
	for (i=0;i<MAX_POINTS;i++){
		bezier[i].c[X]-=minX;
		bezier[i].c[Y]-=minY;
	}

	return EXIT_SUCCESS;
}
int draw_curve() {

	int i;
	float t;
	VERTEX pt;

	glColor3f(0., 1., 0.);

	glBegin( GL_LINE_STRIP);
	// draw the bezier curve using deCasteljau's algorithm
	for (i = 0; i < MAX_POINTS; i++) {
		glVertex3f(bezier[i].c[X], bezier[i].c[Y], bezier[i].c[Z]);
	}
	glEnd();
	// Draw the control points
	glPointSize(5);

	glBegin ( GL_POINTS);

	for (i = 0; i < NUM_CNTL_POINTS; i++) {

		glVertex3f(controlline[i].c[X], controlline[i].c[Y],
				controlline[i].c[Z]);
	}

	glEnd();

	return 0;
}

/* do a linear interpolation between two vertices for a specific t */
void interpolate(VERTEX &newV, VERTEX a, VERTEX b, float t) {
	// ta + (1-t)b
	newV.c[X] = t * a.c[X] + (1 - t) * b.c[X];
	newV.c[Y] = t * a.c[Y] + (1 - t) * b.c[Y];
	newV.c[Z] = t * a.c[Z] + (1 - t) * b.c[Z];
}

// Calculate a point on the Bezier curve
void calc_bezier_point(VERTEX &newV, float t) {
	VERTEX ab, bc, cd, abbc, bccd;
	interpolate(ab, controlline[0], controlline[1], t);
	interpolate(bc, controlline[1], controlline[2], t);
	interpolate(cd, controlline[2], controlline[3], t);

	interpolate(abbc, ab, bc, t);
	interpolate(bccd, bc, cd, t);

	interpolate(newV, abbc, bccd, t);
}

int calculate_normals(int v) {

	int dx;
	float *p1, *p2, *n;

	return 0;
}
